package com.letv.android.client.webview;

import android.text.TextUtils;
import android.view.View;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import com.letv.core.utils.LogInfo;


public class LetvWebViewChromeClient extends WebChromeClient {
    LetvBaseWebViewActivity mBaseWebViewActivity;

    public LetvWebViewChromeClient(LetvBaseWebViewActivity webViewActivity) {
        mBaseWebViewActivity = webViewActivity;
    }

    @Override
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
        LogInfo.log("wlx", "url: " + url + ", message: " + message + ", result: " + result);
        return super.onJsAlert(view, url, message, result);
    }

    @Override
    public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
        LogInfo.log("wlx", "url: " + url + ", message: " + message + ", defaultValue: " + defaultValue + ", result: " + result);
        return super.onJsPrompt(view, url, message, defaultValue, result);
    }

    @Override
    public void onProgressChanged(WebView view, int newProgress) {

        if (!mBaseWebViewActivity.isLoading()) {
            mBaseWebViewActivity.progressBar.setVisibility(View.GONE);
            mBaseWebViewActivity.urlTitleView.setVisibility(View.GONE);
            return;
        }
        //因为凤凰网会一直刷新进度 不显示进度条
        if (mBaseWebViewActivity.getBaseUrl().startsWith("http://v.ifeng.com")) {
            mBaseWebViewActivity.progressBar.setVisibility(View.GONE);
            mBaseWebViewActivity.urlTitleView.setVisibility(View.GONE);
            return;
        }
        if (mBaseWebViewActivity.progressBar.getVisibility() != View.VISIBLE) {
            mBaseWebViewActivity.progressBar.setVisibility(View.VISIBLE);
            mBaseWebViewActivity.urlTitleView.setVisibility(View.VISIBLE);
        }
        if (newProgress > 10 && newProgress < 100) {
            mBaseWebViewActivity.progressBar.setProgress(newProgress);
            mBaseWebViewActivity.loadFinish = false;
        } else if (newProgress == 100) {
            LogInfo.log("wlx", "progress=100");
            if (mBaseWebViewActivity.flag) {
                mBaseWebViewActivity.root.finish();
            } else {//保证错误页面显示
                if (mBaseWebViewActivity.root.getVisibility() != View.VISIBLE) {
                    mBaseWebViewActivity.root.setVisibility(View.VISIBLE);
                }
            }
            mBaseWebViewActivity.progressBar.setVisibility(View.GONE);
            mBaseWebViewActivity.urlTitleView.setVisibility(View.GONE);
            mBaseWebViewActivity.loadFinish = true;
//            if (mBaseWebViewActivity.mShareNewFragment != null) {
//                mBaseWebViewActivity.mShareNewFragment.setIsLoadComplete(mBaseWebViewActivity.loadFinish);
//                mBaseWebViewActivity.mShareNewFragment.notifyShareLayout();
//            }
        }
    }

    /**
     * 用来打印JS的debug日志，比如：console.log console.info console.warn console.error
     *
     * @param message
     * @param lineNumber
     * @param sourceID
     */
    public void onConsoleMessage(String message, int lineNumber, String sourceID) {
        LogInfo.log("wlx", message + " -- From line " + lineNumber + " of " + sourceID);
    }

    @Override
    public void onCloseWindow(WebView window) {
        super.onCloseWindow(window);
        LogInfo.log("wlx", "触发 WebChromeClient 的onCloseWindow 回调");
    }

    @Override
    public void onReceivedTitle(WebView view, String title) {
        if (TextUtils.isEmpty(mBaseWebViewActivity.loadType)) {
            mBaseWebViewActivity.titleView.setText(title);
        } else {
            super.onReceivedTitle(view, title);
        }
        mBaseWebViewActivity.mShareDefaultTitle = title;
    }

    @Override
    public void onReceivedTouchIconUrl(WebView view, String url, boolean precomposed) {
        mBaseWebViewActivity.mShareDefaultIcon = url;
        LogInfo.log("wlx", "url =" + url);
    }
}